package mc2014.hackerthon.Adapters;

import java.util.ArrayList;

import mc2014.hackerthon.sharingphonebook.R;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGridAdapter extends BaseAdapter
{
	private LayoutInflater inflater;
	private Context con;
	private ArrayList<GridItem> gridItemList = new ArrayList<GridItem>();
	private ViewHolder holder;
	
	public CustomGridAdapter(Context context) 
	{
		super();
		con = context;
	}
	@Override
	public int getCount() 
	{
		return gridItemList.size();
	}

	@Override
	public Object getItem(int position) 
	{
		return gridItemList.get(position);
	}

	@Override
	public long getItemId(int position) 
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		if(convertView == null)
		{
			holder = new ViewHolder();
			inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.custom_gridview_layout,null);
			holder.main_grid_img = (ImageView)convertView.findViewById(R.id.main_grid_img);
			holder.main_grid_tv = (TextView)convertView.findViewById(R.id.main_grid_name);
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder)convertView.getTag();
		}
		
		holder.main_grid_img.setImageResource(gridItemList.get(position).getGroupImage());
		holder.main_grid_tv.setText(gridItemList.get(position).getGroupName());
		
		return convertView;
	}
	
	private class ViewHolder
	{
		ImageView main_grid_img;
		TextView main_grid_tv;
	}
	
	public void addItem(int groupImg,String groupName)
	{
		GridItem addGroupItem = new GridItem(groupImg,groupName);
		gridItemList.add(addGroupItem);
		notifyDataSetChanged();
	}
	
	

}
